home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / infrun.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  56KB  |  1,842 lines

  1. /* Start (run) and stop the inferior process, for GDB.
  2.    Copyright (C) 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Notes on the algorithm used in wait_for_inferior to determine if we
  21.    just did a subroutine call when stepping.  We have the following
  22.    information at that point:
  23.  
  24.                   Current and previous (just before this step) pc.
  25.           Current and previous sp.
  26.           Current and previous start of current function.
  27.  
  28.    If the start's of the functions don't match, then
  29.  
  30.        a) We did a subroutine call.
  31.  
  32.    In this case, the pc will be at the beginning of a function.
  33.  
  34.     b) We did a subroutine return.
  35.  
  36.    Otherwise.
  37.  
  38.     c) We did a longjmp.
  39.  
  40.    If we did a longjump, we were doing "nexti", since a next would
  41.    have attempted to skip over the assembly language routine in which
  42.    the longjmp is coded and would have simply been the equivalent of a
  43.    continue.  I consider this ok behaivior.  We'd like one of two
  44.    things to happen if we are doing a nexti through the longjmp()
  45.    routine: 1) It behaves as a stepi, or 2) It acts like a continue as
  46.    above.  Given that this is a special case, and that anybody who
  47.    thinks that the concept of sub calls is meaningful in the context
  48.    of a longjmp, I'll take either one.  Let's see what happens.  
  49.  
  50.    Acts like a subroutine return.  I can handle that with no problem
  51.    at all.
  52.  
  53.    -->So: If the current and previous beginnings of the current
  54.    function don't match, *and* the pc is at the start of a function,
  55.    we've done a subroutine call.  If the pc is not at the start of a
  56.    function, we *didn't* do a subroutine call.  
  57.  
  58.    -->If the beginnings of the current and previous function do match,
  59.    either: 
  60.  
  61.        a) We just did a recursive call.
  62.  
  63.        In this case, we would be at the very beginning of a
  64.        function and 1) it will have a prologue (don't jump to
  65.        before prologue, or 2) (we assume here that it doesn't have
  66.        a prologue) there will have been a change in the stack
  67.        pointer over the last instruction.  (Ie. it's got to put
  68.        the saved pc somewhere.  The stack is the usual place.  In
  69.        a recursive call a register is only an option if there's a
  70.        prologue to do something with it.  This is even true on
  71.        register window machines; the prologue sets up the new
  72.        window.  It might not be true on a register window machine
  73.        where the call instruction moved the register window
  74.        itself.  Hmmm.  One would hope that the stack pointer would
  75.        also change.  If it doesn't, somebody send me a note, and
  76.        I'll work out a more general theory.
  77.        bug-gdb@prep.ai.mit.edu).  This is true (albeit slipperly
  78.        so) on all machines I'm aware of:
  79.  
  80.           m68k:    Call changes stack pointer.  Regular jumps don't.
  81.  
  82.           sparc:    Recursive calls must have frames and therefor,
  83.                     prologues.
  84.  
  85.           vax:    All calls have frames and hence change the
  86.                     stack pointer.
  87.  
  88.     b) We did a return from a recursive call.  I don't see that we
  89.        have either the ability or the need to distinguish this
  90.        from an ordinary jump.  The stack frame will be printed
  91.        when and if the frame pointer changes; if we are in a
  92.        function without a frame pointer, it's the users own
  93.        lookout.
  94.  
  95.     c) We did a jump within a function.  We assume that this is
  96.        true if we didn't do a recursive call.
  97.  
  98.     d) We are in no-man's land ("I see no symbols here").  We
  99.        don't worry about this; it will make calls look like simple
  100.        jumps (and the stack frames will be printed when the frame
  101.        pointer moves), which is a reasonably non-violent response.
  102.  
  103. #if 0
  104.     We skip this; it causes more problems than it's worth.
  105. #ifdef SUN4_COMPILER_FEATURE
  106.     We do a special ifdef for the sun 4, forcing it to single step
  107.   into calls which don't have prologues.  This means that we can't
  108.   nexti over leaf nodes, we can probably next over them (since they
  109.   won't have debugging symbols, usually), and we can next out of
  110.   functions returning structures (with a "call .stret4" at the end).
  111. #endif
  112. #endif
  113. */
  114.    
  115.  
  116.    
  117.    
  118.  
  119. #include <stdio.h>
  120. #include <string.h>
  121. #include "defs.h"
  122. #include "param.h"
  123. #include "symtab.h"
  124. #include "frame.h"
  125. #include "inferior.h"
  126. #include "breakpoint.h"
  127. #include "wait.h"
  128. #include "gdbcore.h"
  129. #include "signame.h"
  130. #include "command.h"
  131. #include "terminal.h"        /* For #ifdef TIOCGPGRP and new_tty */
  132. #include "target.h"
  133.  
  134. #include <signal.h>
  135.  
  136. /* unistd.h is needed to #define X_OK */
  137. #ifdef USG
  138. #include <unistd.h>
  139. #else
  140. #include <sys/file.h>
  141. #endif
  142.  
  143. #ifdef SET_STACK_LIMIT_HUGE
  144. #include <sys/time.h>
  145. #include <sys/resource.h>
  146.  
  147. extern int original_stack_limit;
  148. #endif /* SET_STACK_LIMIT_HUGE */
  149.  
  150. extern char *getenv ();
  151. extern char **environ;
  152.  
  153. extern struct target_ops child_ops;    /* In inftarg.c */
  154.  
  155.  
  156. /* Sigtramp is a routine that the kernel calls (which then calls the
  157.    signal handler).  On most machines it is a library routine that
  158.    is linked into the executable.
  159.  
  160.    This macro, given a program counter value and the name of the
  161.    function in which that PC resides (which can be null if the
  162.    name is not known), returns nonzero if the PC and name show
  163.    that we are in sigtramp.
  164.  
  165.    On most machines just see if the name is sigtramp (and if we have
  166.    no name, assume we are not in sigtramp).  */
  167. #if !defined (IN_SIGTRAMP)
  168. #define IN_SIGTRAMP(pc, name) \
  169.   (name && !strcmp ("_sigtramp", name))
  170. #endif
  171.  
  172. #ifdef TDESC
  173. #include "tdesc.h"
  174. int safe_to_init_tdesc_context = 0;
  175. extern dc_dcontext_t current_context;
  176. #endif
  177.  
  178. /* Tables of how to react to signals; the user sets them.  */
  179.  
  180. static char signal_stop[NSIG];
  181. static char signal_print[NSIG];
  182. static char signal_program[NSIG];
  183.  
  184. /* Nonzero if breakpoints are now inserted in the inferior.  */
  185. /* Nonstatic for initialization during xxx_create_inferior. FIXME. */
  186.  
  187. /*static*/ int breakpoints_inserted;
  188.  
  189. /* Function inferior was in as of last step command.  */
  190.  
  191. static struct symbol *step_start_function;
  192.  
  193. /* Nonzero => address for special breakpoint for resuming stepping.  */
  194.  
  195. static CORE_ADDR step_resume_break_address;
  196.  
  197. /* Pointer to orig contents of the byte where the special breakpoint is.  */
  198.  
  199. static char step_resume_break_shadow[BREAKPOINT_MAX];
  200.  
  201. /* Nonzero means the special breakpoint is a duplicate
  202.    so it has not itself been inserted.  */
  203.  
  204. static int step_resume_break_duplicate;
  205.  
  206. /* Nonzero if we are expecting a trace trap and should proceed from it.  */
  207.  
  208. static int trap_expected;
  209.  
  210. /* Nonzero if the next time we try to continue the inferior, it will
  211.    step one instruction and generate a spurious trace trap.
  212.    This is used to compensate for a bug in HP-UX.  */
  213.  
  214. static int trap_expected_after_continue;
  215.  
  216. /* Nonzero means expecting a trace trap
  217.    and should stop the inferior and return silently when it happens.  */
  218.  
  219. int stop_after_trap;
  220.  
  221. /* Nonzero means expecting a trap and caller will handle it themselves.
  222.    It is used after attach, due to attaching to a process;
  223.    when running in the shell before the child program has been exec'd;
  224.    and when running some kinds of remote stuff (FIXME?).  */
  225.  
  226. int stop_soon_quietly;
  227.  
  228. /* Nonzero if pc has been changed by the debugger
  229.    since the inferior stopped.  */
  230.  
  231. int pc_changed;
  232.  
  233. /* Nonzero if proceed is being used for a "finish" command or a similar
  234.    situation when stop_registers should be saved.  */
  235.  
  236. int proceed_to_finish;
  237.  
  238. /* Save register contents here when about to pop a stack dummy frame,
  239.    if-and-only-if proceed_to_finish is set.
  240.    Thus this contains the return value from the called function (assuming
  241.    values are returned in a register).  */
  242.  
  243. char stop_registers[REGISTER_BYTES];
  244.  
  245. /* Nonzero if program stopped due to error trying to insert breakpoints.  */
  246.  
  247. static int breakpoints_failed;
  248.  
  249. /* Nonzero after stop if current stack frame should be printed.  */
  250.  
  251. static int stop_print_frame;
  252.  
  253. #ifdef NO_SINGLE_STEP
  254. extern int one_stepped;        /* From machine dependent code */
  255. extern void single_step ();    /* Same. */
  256. #endif /* NO_SINGLE_STEP */
  257.  
  258. static void insert_step_breakpoint ();
  259. static void remove_step_breakpoint ();
  260. /*static*/ void wait_for_inferior ();
  261. void init_wait_for_inferior ();
  262. void normal_stop ();
  263.  
  264.  
  265. /* Things to clean up if we QUIT out of resume ().  */
  266. /* ARGSUSED */
  267. static void
  268. resume_cleanups (arg)
  269.      int arg;
  270. {
  271.   normal_stop ();
  272. }
  273.  
  274. /* Resume the inferior, but allow a QUIT.  This is useful if the user
  275.    wants to interrupt some lengthy single-stepping operation
  276.    (for child processes, the SIGINT goes to the inferior, and so
  277.    we get a SIGINT random_signal, but for remote debugging and perhaps
  278.    other targets, that's not true).
  279.  
  280.    STEP nonzero if we should step (zero to continue instead).
  281.    SIG is the signal to give the inferior (zero for none).  */
  282. static void
  283. resume (step, sig)
  284.      int step;
  285.      int sig;
  286. {
  287.   struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
  288.   QUIT;
  289.  
  290. #ifdef NO_SINGLE_STEP
  291.   if (step) {
  292.     single_step();    /* Do it the hard way, w/temp breakpoints */
  293.     step = 0;        /* ...and don't ask hardware to do it.  */
  294.   }
  295. #endif
  296.  
  297.   /* Handle any optimized stores to the inferior NOW...  */
  298. #ifdef DO_DEFERRED_STORES
  299.   DO_DEFERRED_STORES;
  300. #endif
  301.  
  302.   target_resume (step, sig);
  303.   discard_cleanups (old_cleanups);
  304. }
  305.  
  306.  
  307. /* Clear out all variables saying what to do when inferior is continued.
  308.    First do this, then set the ones you want, then call `proceed'.  */
  309.  
  310. void
  311. clear_proceed_status ()
  312. {
  313.   trap_expected = 0;
  314.   step_range_start = 0;
  315.   step_range_end = 0;
  316.   step_frame_address = 0;
  317.   step_over_calls = -1;
  318.   step_resume_break_address = 0;
  319.   stop_after_trap = 0;
  320.   stop_soon_quietly = 0;
  321.   proceed_to_finish = 0;
  322.   breakpoint_proceeded = 1;    /* We're about to proceed... */
  323.  
  324.   /* Discard any remaining commands or status from previous stop.  */
  325.   bpstat_clear (&stop_bpstat);
  326. }
  327.  
  328. /* Basic routine for continuing the program in various fashions.
  329.  
  330.    ADDR is the address to resume at, or -1 for resume where stopped.
  331.    SIGGNAL is the signal to give it, or 0 for none,
  332.      or -1 for act according to how it stopped.
  333.    STEP is nonzero if should trap after one instruction.
  334.      -1 means return after that and print nothing.
  335.      You should probably set various step_... variables
  336.      before calling here, if you are stepping.
  337.  
  338.    You should call clear_proceed_status before calling proceed.  */
  339.  
  340. void
  341. proceed (addr, siggnal, step)
  342.      CORE_ADDR addr;
  343.      int siggnal;
  344.      int step;
  345. {
  346.   int oneproc = 0;
  347.  
  348.   if (step > 0)
  349.     step_start_function = find_pc_function (read_pc ());
  350.   if (step < 0)
  351.     stop_after_trap = 1;
  352.  
  353.   if (addr == (CORE_ADDR)-1)
  354.     {
  355.       /* If there is a breakpoint at the address we will resume at,
  356.      step one instruction before inserting breakpoints
  357.      so that we do not stop right away.  */
  358.  
  359.       if (!pc_changed && breakpoint_here_p (read_pc ()))
  360.     oneproc = 1;
  361.     }
  362.   else
  363.     {
  364.       write_register (PC_REGNUM, addr);
  365. #ifdef NPC_REGNUM
  366.       write_register (NPC_REGNUM, addr + 4);
  367. #ifdef NNPC_REGNUM
  368.       write_register (NNPC_REGNUM, addr + 8);
  369. #endif
  370. #endif
  371.     }
  372.  
  373.   if (trap_expected_after_continue)
  374.     {
  375.       /* If (step == 0), a trap will be automatically generated after
  376.      the first instruction is executed.  Force step one
  377.      instruction to clear this condition.  This should not occur
  378.      if step is nonzero, but it is harmless in that case.  */
  379.       oneproc = 1;
  380.       trap_expected_after_continue = 0;
  381.     }
  382.  
  383.   if (oneproc)
  384.     /* We will get a trace trap after one instruction.
  385.        Continue it automatically and insert breakpoints then.  */
  386.     trap_expected = 1;
  387.   else
  388.     {
  389.       int temp = insert_breakpoints ();
  390.       if (temp)
  391.     {
  392.       print_sys_errmsg ("ptrace", temp);
  393.       error ("Cannot insert breakpoints.\n\
  394. The same program may be running in another process.");
  395.     }
  396.       breakpoints_inserted = 1;
  397.     }
  398.  
  399.   /* Install inferior's terminal modes.  */
  400.   target_terminal_inferior ();
  401.  
  402.   if (siggnal >= 0)
  403.     stop_signal = siggnal;
  404.   /* If this signal should not be seen by program,
  405.      give it zero.  Used for debugging signals.  */
  406.   else if (stop_signal < NSIG && !signal_program[stop_signal])
  407.     stop_signal= 0;
  408.  
  409.   /* Resume inferior.  */
  410.   resume (oneproc || step || bpstat_should_step (), stop_signal);
  411.  
  412.   /* Wait for it to stop (if not standalone)
  413.      and in any case decode why it stopped, and act accordingly.  */
  414.  
  415.   wait_for_inferior ();
  416.   normal_stop ();
  417. }
  418.  
  419. #if 0
  420. /* This might be useful (not sure), but isn't currently used.  See also
  421.    write_pc().  */
  422. /* Writing the inferior pc as a register calls this function
  423.    to inform infrun that the pc has been set in the debugger.  */
  424.  
  425. void
  426. writing_pc (val)
  427.      CORE_ADDR val;
  428. {
  429.   stop_pc = val;
  430.   pc_changed = 1;
  431. }
  432. #endif
  433.  
  434. /* Record the pc and sp of the program the last time it stopped.
  435.    These are just used internally by wait_for_inferior, but need
  436.    to be preserved over calls to it and cleared when the inferior
  437.    is started.  */
  438. static CORE_ADDR prev_pc;
  439. static CORE_ADDR prev_sp;
  440. static CORE_ADDR prev_func_start;
  441. static char *prev_func_name;
  442.  
  443.  
  444. /* Start an inferior Unix child process and sets inferior_pid to its pid.
  445.    EXEC_FILE is the file to run.
  446.    ALLARGS is a string containing the arguments to the program.
  447.    ENV is the environment vector to pass.  Errors reported with error().  */
  448.  
  449. #ifndef SHELL_FILE
  450. #define SHELL_FILE "/bin/sh"
  451. #endif
  452.  
  453. void
  454. child_create_inferior (exec_file, allargs, env)
  455.      char *exec_file;
  456.      char *allargs;
  457.      char **env;
  458. {
  459.   int pid;
  460.   char *shell_command;
  461.   extern int sys_nerr;
  462.   extern char *sys_errlist[];
  463.   char *shell_file;
  464.   static char default_shell_file[] = SHELL_FILE;
  465.   int len;
  466.   int pending_execs;
  467.   /* Set debug_fork then attach to the child while it sleeps, to debug. */
  468.   static int debug_fork = 0;
  469.   /* This is set to the result of setpgrp, which if vforked, will be visible
  470.      to you in the parent process.  It's only used by humans for debugging.  */
  471.   static int debug_setpgrp = 657473;
  472.   char **save_our_env;
  473.  
  474.   /* The user might want tilde-expansion, and in general probably wants
  475.      the program to behave the same way as if run from
  476.      his/her favorite shell.  So we let the shell run it for us.
  477.      FIXME, this should probably search the local environment (as
  478.      modified by the setenv command), not the env gdb inherited.  */
  479.   shell_file = getenv ("SHELL");
  480.   if (shell_file == NULL)
  481.     shell_file = default_shell_file;
  482.   
  483.   len = 5 + strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 10;
  484.   /* If desired, concat something onto the front of ALLARGS.
  485.      SHELL_COMMAND is the result.  */
  486. #ifdef SHELL_COMMAND_CONCAT
  487.   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
  488.   strcpy (shell_command, SHELL_COMMAND_CONCAT);
  489. #else
  490.   shell_command = (char *) alloca (len);
  491.   shell_command[0] = '\0';
  492. #endif
  493.   strcat (shell_command, "exec ");
  494.   strcat (shell_command, exec_file);
  495.   strcat (shell_command, " ");
  496.   strcat (shell_command, allargs);
  497.  
  498.   /* exec is said to fail if the executable is open.  */
  499.   close_exec_file ();
  500.  
  501.   /* Retain a copy of our environment variables, since the child will
  502.      replace the value of  environ  and if we're vforked, we have to 
  503.      restore it.  */
  504.   save_our_env = environ;
  505.  
  506.   /* Tell the terminal handling subsystem what tty we plan to run on;
  507.      it will just record the information for later.  */
  508.  
  509.   new_tty_prefork (inferior_io_terminal);
  510.  
  511.   /* It is generally good practice to flush any possible pending stdio
  512.      output prior to doing a fork, to avoid the possibility of both the
  513.      parent and child flushing the same data after the fork. */
  514.  
  515.   fflush (stdout);
  516.   fflush (stderr);
  517.  
  518. #if defined(USG) && !defined(HAVE_VFORK)
  519.   pid = fork ();
  520. #else
  521.   if (debug_fork)
  522.     pid = fork ();
  523.   else
  524.     pid = vfork ();
  525. #endif
  526.  
  527.   if (pid < 0)
  528.     perror_with_name ("vfork");
  529.  
  530.   if (pid == 0)
  531.     {
  532.       if (debug_fork) 
  533.     sleep (debug_fork);
  534.  
  535. #ifdef TIOCGPGRP
  536.       /* Run inferior in a separate process group.  */
  537.       debug_setpgrp = setpgrp (getpid (), getpid ());
  538.       if (debug_setpgrp == -1)
  539.      perror("setpgrp failed in child");
  540. #endif /* TIOCGPGRP */
  541.  
  542. #ifdef SET_STACK_LIMIT_HUGE
  543.       /* Reset the stack limit back to what it was.  */
  544.       {
  545.     struct rlimit rlim;
  546.  
  547.     getrlimit (RLIMIT_STACK, &rlim);
  548.     rlim.rlim_cur = original_stack_limit;
  549.     setrlimit (RLIMIT_STACK, &rlim);
  550.       }
  551. #endif /* SET_STACK_LIMIT_HUGE */
  552.  
  553.       /* Ask the tty subsystem to switch to the one we specified earlier
  554.      (or to share the current terminal, if none was specified).  */
  555.  
  556.       new_tty ();
  557.  
  558.       /* Changing the signal handlers for the inferior after
  559.      a vfork can also change them for the superior, so we don't mess
  560.      with signals here.  See comments in
  561.      initialize_signals for how we get the right signal handlers
  562.      for the inferior.  */
  563.  
  564.       call_ptrace (0, 0, 0, 0);        /* "Trace me, Dr. Memory!" */
  565.  
  566.       /* There is no execlpe call, so we have to set the environment
  567.      for our child in the global variable.  If we've vforked, this
  568.      clobbers the parent, but environ is restored a few lines down
  569.      in the parent.  By the way, yes we do need to look down the
  570.      path to find $SHELL.  Rich Pixley says so, and I agree.  */
  571.       environ = env;
  572.       execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
  573.  
  574.       fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
  575.            errno < sys_nerr ? sys_errlist[errno] : "unknown error");
  576.       fflush (stderr);
  577.       _exit (0177);
  578.     }
  579.  
  580.   /* Restore our environment in case a vforked child clob'd it.  */
  581.   environ = save_our_env;
  582.  
  583.   /* Now that we have a child process, make it our target.  */
  584.   push_target (&child_ops);
  585.  
  586. #ifdef CREATE_INFERIOR_HOOK
  587.   CREATE_INFERIOR_HOOK (pid);
  588. #endif  
  589.  
  590. /* The process was started by the fork that created it,
  591.    but it will have stopped one instruction after execing the shell.
  592.    Here we must get it up to actual execution of the real program.  */
  593.  
  594.   inferior_pid = pid;        /* Needed for wait_for_inferior stuff below */
  595.  
  596.   clear_proceed_status ();
  597.  
  598. #if defined (START_INFERIOR_HOOK)
  599.   START_INFERIOR_HOOK ();
  600. #endif
  601.  
  602.   /* We will get a trace trap after one instruction.
  603.      Continue it automatically.  Eventually (after shell does an exec)
  604.      it will get another trace trap.  Then insert breakpoints and continue.  */
  605.  
  606. #ifdef START_INFERIOR_TRAPS_EXPECTED
  607.   pending_execs = START_INFERIOR_TRAPS_EXPECTED;
  608. #else
  609.   pending_execs = 2;
  610. #endif
  611.  
  612.   init_wait_for_inferior ();
  613.  
  614.   /* Set up the "saved terminal modes" of the inferior
  615.      based on what modes we are starting it with.  */
  616.   target_terminal_init ();
  617.  
  618.   /* Install inferior's terminal modes.  */
  619.   target_terminal_inferior ();
  620.  
  621.   while (1)
  622.     {
  623.       stop_soon_quietly = 1;    /* Make wait_for_inferior be quiet */
  624.       wait_for_inferior ();
  625.       if (stop_signal != SIGTRAP)
  626.     {
  627.       /* Let shell child handle its own signals in its own way */
  628.       /* FIXME, what if child has exit()ed?  Must exit loop somehow */
  629.       resume (0, stop_signal);
  630.     }
  631.       else
  632.     {
  633.       /* We handle SIGTRAP, however; it means child did an exec.  */
  634.       if (0 == --pending_execs)
  635.         break;
  636.       resume (0, 0);        /* Just make it go on */
  637.     }
  638.     }
  639.   stop_soon_quietly = 0;
  640.  
  641.   /* We are now in the child process of interest, having exec'd the
  642.      correct program, and are poised at the first instruction of the
  643.      new program.  */
  644. #ifdef SOLIB_CREATE_INFERIOR_HOOK
  645.   SOLIB_CREATE_INFERIOR_HOOK ();
  646. #endif
  647.  
  648.   /* Should this perhaps just be a "proceed" call?  FIXME */
  649.   insert_step_breakpoint ();
  650.   breakpoints_failed = insert_breakpoints ();
  651.   if (!breakpoints_failed)
  652.     {
  653.       breakpoints_inserted = 1;
  654.       target_terminal_inferior();
  655.       /* Start the child program going on its first instruction, single-
  656.      stepping if we need to.  */
  657.       resume (bpstat_should_step (), 0);
  658.       wait_for_inferior ();
  659.       normal_stop ();
  660.     }
  661. }
  662.  
  663. /* Start remote-debugging of a machine over a serial link.  */
  664.  
  665. void
  666. start_remote ()
  667. {
  668.   init_wait_for_inferior ();
  669.   clear_proceed_status ();
  670.   stop_soon_quietly = 1;
  671.   trap_expected = 0;
  672.   wait_for_inferior ();
  673.   normal_stop ();
  674. }
  675.  
  676. /* Initialize static vars when a new inferior begins.  */
  677.  
  678. void
  679. init_wait_for_inferior ()
  680. {
  681.   /* These are meaningless until the first time through wait_for_inferior.  */
  682.   prev_pc = 0;
  683.   prev_sp = 0;
  684.   prev_func_start = 0;
  685.   prev_func_name = NULL;
  686.  
  687.   trap_expected_after_continue = 0;
  688.   breakpoints_inserted = 0;
  689.   mark_breakpoints_out ();
  690.   stop_signal = 0;        /* Don't confuse first call to proceed(). */
  691. }
  692.  
  693.  
  694. /* Attach to process PID, then initialize for debugging it
  695.    and wait for the trace-trap that results from attaching.  */
  696.  
  697. void
  698. child_attach (args, from_tty)
  699.      char *args;
  700.      int from_tty;
  701. {
  702.   char *exec_file;
  703.   int pid;
  704.  
  705.   dont_repeat();
  706.  
  707.   if (!args)
  708.     error_no_arg ("process-id to attach");
  709.  
  710. #ifndef ATTACH_DETACH
  711.   error ("Can't attach to a process on this machine.");
  712. #else
  713.   pid = atoi (args);
  714.  
  715.   if (target_has_execution)
  716.     {
  717.       if (query ("A program is being debugged already.  Kill it? "))
  718.     target_kill ((char *)0, from_tty);
  719.       else
  720.     error ("Inferior not killed.");
  721.     }
  722.  
  723.   exec_file = (char *) get_exec_file (1);
  724.  
  725.   if (from_tty)
  726.     {
  727.       printf ("Attaching program: %s pid %d\n",
  728.           exec_file, pid);
  729.       fflush (stdout);
  730.     }
  731.  
  732.   attach (pid);
  733.   inferior_pid = pid;
  734.   push_target (&child_ops);
  735.  
  736.   mark_breakpoints_out ();
  737.   target_terminal_init ();
  738.   clear_proceed_status ();
  739.   stop_soon_quietly = 1;
  740.   /*proceed (-1, 0, -2);*/
  741.   target_terminal_inferior ();
  742.   wait_for_inferior ();
  743. #ifdef SOLIB_ADD
  744.   SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0);
  745. #endif
  746.   normal_stop ();
  747. #endif  /* ATTACH_DETACH */
  748. }
  749.  
  750. /* Wait for control to return from inferior to debugger.
  751.    If inferior gets a signal, we may decide to start it up again
  752.    instead of returning.  That is why there is a loop in this function.
  753.    When this function actually returns it means the inferior
  754.    should be left stopped and GDB should read more commands.  */
  755.  
  756. void
  757. wait_for_inferior ()
  758. {
  759.   WAITTYPE w;
  760.   int another_trap;
  761.   int random_signal;
  762.   CORE_ADDR stop_sp;
  763.   CORE_ADDR stop_func_start;
  764.   char *stop_func_name;
  765.   CORE_ADDR prologue_pc;
  766.   int stop_step_resume_break;
  767.   struct symtab_and_line sal;
  768.   int remove_breakpoints_on_following_step = 0;
  769. #ifdef TDESC
  770.   extern dc_handle_t tdesc_handle;
  771. #endif
  772.   int current_line;
  773.  
  774. #if 0
  775.   /* This no longer works now that read_register is lazy;
  776.      it might try to ptrace when the process is not stopped.  */
  777.   prev_pc = read_pc ();
  778.   (void) find_pc_partial_function (prev_pc, &prev_func_name,
  779.                    &prev_func_start);
  780.   prev_func_start += FUNCTION_START_OFFSET;
  781.   prev_sp = read_register (SP_REGNUM);
  782. #endif /* 0 */
  783.  
  784.   sal = find_pc_line(prev_pc, 0);
  785.   current_line = sal.line;
  786.  
  787.   while (1)
  788.     {
  789.       /* Clean up saved state that will become invalid.  */
  790.       pc_changed = 0;
  791.       flush_cached_frames ();
  792.       registers_changed ();
  793.  
  794.       target_wait (&w);
  795.  
  796.       /* See if the process still exists; clean up if it doesn't.  */
  797.       if (WIFEXITED (w))
  798.     {
  799.       target_terminal_ours ();    /* Must do this before mourn anyway */
  800. #ifdef TDESC 
  801.           safe_to_init_tdesc_context = 0;
  802. #endif
  803.       if (WEXITSTATUS (w))
  804.         printf ("\nProgram exited with code 0%o.\n", 
  805.              (unsigned int)WEXITSTATUS (w));
  806.       else
  807.         if (!batch_mode())
  808.           printf ("\nProgram exited normally.\n");
  809.       fflush (stdout);
  810.       target_mourn_inferior ();
  811. #ifdef NO_SINGLE_STEP
  812.       one_stepped = 0;
  813. #endif
  814.       stop_print_frame = 0;
  815.       break;
  816.     }
  817.       else if (!WIFSTOPPED (w))
  818.     {
  819.       stop_print_frame = 0;
  820.       stop_signal = WTERMSIG (w);
  821.       target_terminal_ours ();    /* Must do this before mourn anyway */
  822.       target_kill ((char *)0, 0);    /* kill mourns as well */
  823. #ifdef TDESC
  824.           safe_to_init_tdesc_context = 0;
  825. #endif
  826. #ifdef PRINT_RANDOM_SIGNAL
  827.       printf ("\nProgram terminated: ");
  828.       PRINT_RANDOM_SIGNAL (stop_signal);
  829. #else
  830.       printf ("\nProgram terminated with signal %d, %s\n",
  831.           stop_signal,
  832.           stop_signal < NSIG
  833.           ? sys_siglist[stop_signal]
  834.           : "(undocumented)");
  835. #endif
  836.       printf ("The inferior process no longer exists.\n");
  837.       fflush (stdout);
  838. #ifdef NO_SINGLE_STEP
  839.       one_stepped = 0;
  840. #endif
  841.       break;
  842.     }
  843.       
  844. #ifdef NO_SINGLE_STEP
  845.       if (one_stepped)
  846.     single_step (0);    /* This actually cleans up the ss */
  847. #endif /* NO_SINGLE_STEP */
  848.       
  849.       stop_pc = read_pc ();
  850. #ifdef TDESC
  851.       if (safe_to_init_tdesc_context)   
  852.         {
  853.       current_context = init_dcontext();
  854.           set_current_frame ( create_new_frame (get_frame_base (read_pc()),read_pc()));
  855.         }
  856.       else
  857. #endif /* TDESC */
  858.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  859.                         read_pc ()));
  860.       
  861.       stop_frame_address = FRAME_FP (get_current_frame ());
  862.       stop_sp = read_register (SP_REGNUM);
  863.       stop_func_start = 0;
  864.       stop_func_name = 0;
  865.       /* Don't care about return value; stop_func_start and stop_func_name
  866.      will both be 0 if it doesn't work.  */
  867.       (void) find_pc_partial_function (stop_pc, &stop_func_name,
  868.                        &stop_func_start);
  869.       stop_func_start += FUNCTION_START_OFFSET;
  870.       another_trap = 0;
  871.       bpstat_clear (&stop_bpstat);
  872.       stop_step = 0;
  873.       stop_stack_dummy = 0;
  874.       stop_print_frame = 1;
  875.       stop_step_resume_break = 0;
  876.       random_signal = 0;
  877.       stopped_by_random_signal = 0;
  878.       breakpoints_failed = 0;
  879.       
  880.       /* Look at the cause of the stop, and decide what to do.
  881.      The alternatives are:
  882.      1) break; to really stop and return to the debugger,
  883.      2) drop through to start up again
  884.      (set another_trap to 1 to single step once)
  885.      3) set random_signal to 1, and the decision between 1 and 2
  886.      will be made according to the signal handling tables.  */
  887.       
  888.       stop_signal = WSTOPSIG (w);
  889.       
  890.       /* First, distinguish signals caused by the debugger from signals
  891.      that have to do with the program's own actions.
  892.      Note that breakpoint insns may cause SIGTRAP or SIGILL
  893.      or SIGEMT, depending on the operating system version.
  894.      Here we detect when a SIGILL or SIGEMT is really a breakpoint
  895.      and change it to SIGTRAP.  */
  896.       
  897.       if (stop_signal == SIGTRAP
  898.       || (breakpoints_inserted &&
  899.           (stop_signal == SIGILL
  900.            || stop_signal == SIGEMT))
  901.       || stop_soon_quietly)
  902.     {
  903.       if (stop_signal == SIGTRAP && stop_after_trap)
  904.         {
  905.           stop_print_frame = 0;
  906.           break;
  907.         }
  908.       if (stop_soon_quietly)
  909.         break;
  910.  
  911.       /* Don't even think about breakpoints
  912.          if just proceeded over a breakpoint.
  913.  
  914.          However, if we are trying to proceed over a breakpoint
  915.          and end up in sigtramp, then step_resume_break_address
  916.          will be set and we should check whether we've hit the
  917.          step breakpoint.  */
  918.       if (stop_signal == SIGTRAP && trap_expected
  919.           && step_resume_break_address == NULL)
  920.         bpstat_clear (&stop_bpstat);
  921.       else
  922.         {
  923.           /* See if there is a breakpoint at the current PC.  */
  924. #if DECR_PC_AFTER_BREAK
  925.           /* Notice the case of stepping through a jump
  926.          that leads just after a breakpoint.
  927.          Don't confuse that with hitting the breakpoint.
  928.          What we check for is that 1) stepping is going on
  929.          and 2) the pc before the last insn does not match
  930.          the address of the breakpoint before the current pc.  */
  931.           if (!(prev_pc != stop_pc - DECR_PC_AFTER_BREAK
  932.             && step_range_end && !step_resume_break_address))
  933. #endif /* DECR_PC_AFTER_BREAK not zero */
  934.         {
  935.           /* See if we stopped at the special breakpoint for
  936.              stepping over a subroutine call.  If both are zero,
  937.              this wasn't the reason for the stop.  */
  938.           if (stop_pc - DECR_PC_AFTER_BREAK
  939.                         == step_resume_break_address
  940.               && step_resume_break_address)
  941.             {
  942.               stop_step_resume_break = 1;
  943.               if (DECR_PC_AFTER_BREAK)
  944.             {
  945.               stop_pc -= DECR_PC_AFTER_BREAK;
  946.               write_register (PC_REGNUM, stop_pc);
  947.               pc_changed = 0;
  948.             }
  949.             }
  950.           else
  951.             {
  952.               stop_bpstat =
  953.             bpstat_stop_status (&stop_pc, stop_frame_address);
  954.               /* Following in case break condition called a
  955.              function.  */
  956.               stop_print_frame = 1;
  957.             }
  958.         }
  959.         }
  960.       
  961.       if (stop_signal == SIGTRAP)
  962.         random_signal
  963.           = !(bpstat_explains_signal (stop_bpstat)
  964.           || trap_expected
  965.           || stop_step_resume_break
  966.           || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
  967.           || (step_range_end && !step_resume_break_address));
  968.       else
  969.         {
  970.           random_signal
  971.         = !(bpstat_explains_signal (stop_bpstat)
  972.             || stop_step_resume_break
  973.             /* End of a stack dummy.  Some systems (e.g. Sony
  974.                news) give another signal besides SIGTRAP,
  975.                so check here as well as above.  */
  976.             || (stop_sp INNER_THAN stop_pc
  977.             && stop_pc INNER_THAN stop_frame_address)
  978.             );
  979.           if (!random_signal)
  980.         stop_signal = SIGTRAP;
  981.         }
  982.     }
  983.       else
  984.     random_signal = 1;
  985.       
  986.       /* For the program's own signals, act according to
  987.      the signal handling tables.  */
  988.       
  989.       if (random_signal)
  990.     {
  991.       /* Signal not for debugging purposes.  */
  992.       int printed = 0;
  993.       
  994.       stopped_by_random_signal = 1;
  995.       
  996.       if (stop_signal >= NSIG
  997.           || signal_print[stop_signal])
  998.         {
  999.           printed = 1;
  1000.           target_terminal_ours_for_output ();
  1001.  
  1002.           /* HEY! WCL stuff.... */
  1003.           if (stop_signal == SIGUSR1) {
  1004.         FILE *input;
  1005.         char cmd[1024],path[1024];
  1006.  
  1007.         sprintf(path,"/tmp/add-file-command-%d\0",inferior_pid);
  1008.         input = fopen(path,"r");
  1009.         if (input == NULL) {
  1010.           printf("GDB: Cannot find add-file-commands\n");
  1011.         } else {
  1012.           fgets(cmd,1024,input);
  1013.           add_symbol_file_command(cmd,0);
  1014.           fclose(input);
  1015.         }
  1016.           } else {
  1017. #ifdef PRINT_RANDOM_SIGNAL
  1018.         PRINT_RANDOM_SIGNAL (stop_signal);
  1019. #else
  1020.         printf ("\nProgram received signal %d, %s\n",
  1021.             stop_signal,
  1022.             stop_signal < NSIG
  1023.             ? sys_siglist[stop_signal]
  1024.             : "(undocumented)");
  1025. #endif                /* PRINT_RANDOM_SIGNAL */
  1026.         fflush (stdout);
  1027.           }
  1028.         }
  1029.       if (stop_signal >= NSIG
  1030.           || signal_stop[stop_signal])
  1031.         break;
  1032.       /* If not going to stop, give terminal back
  1033.          if we took it away.  */
  1034.       else if (printed)
  1035.         target_terminal_inferior ();
  1036.  
  1037.       /* Note that virtually all the code below does `if !random_signal'.
  1038.          Perhaps this code should end with a goto or continue.  At least
  1039.          one (now fixed) bug was caused by this -- a !random_signal was
  1040.          missing in one of the tests below.  */
  1041.     }
  1042.       
  1043.       /* Handle cases caused by hitting a breakpoint.  */
  1044.       
  1045.       if (!random_signal
  1046.       && (bpstat_explains_signal (stop_bpstat) || stop_step_resume_break))
  1047.     {
  1048.       /* Does a breakpoint want us to stop?  */
  1049.       if (bpstat_stop (stop_bpstat))
  1050.         {
  1051.           stop_print_frame = bpstat_should_print (stop_bpstat);
  1052.           break;
  1053.         }
  1054.       /* But if we have hit the step-resumption breakpoint,
  1055.          remove it.  It has done its job getting us here.
  1056.          The sp test is to make sure that we don't get hung
  1057.          up in recursive calls in functions without frame
  1058.          pointers.  If the stack pointer isn't outside of
  1059.          where the breakpoint was set (within a routine to be
  1060.          stepped over), we're in the middle of a recursive
  1061.          call. Not true for reg window machines (sparc)
  1062.          because the must change frames to call things and
  1063.          the stack pointer doesn't have to change if it
  1064.          the bp was set in a routine without a frame (pc can
  1065.          be stored in some other window).
  1066.          
  1067.          The removal of the sp test is to allow calls to
  1068.          alloca.  Nasty things were happening.  Oh, well,
  1069.          gdb can only handle one level deep of lack of
  1070.          frame pointer. */
  1071.       if (stop_step_resume_break
  1072.           && (step_frame_address == 0
  1073.           || (stop_frame_address == step_frame_address)))
  1074.         {
  1075.           remove_step_breakpoint ();
  1076.           step_resume_break_address = 0;
  1077.  
  1078.           /* If were waiting for a trap, hitting the step_resume_break
  1079.          doesn't count as getting it.  */
  1080.           if (trap_expected)
  1081.         another_trap = 1;
  1082.         }
  1083.       /* Otherwise, must remove breakpoints and single-step
  1084.          to get us past the one we hit.  */
  1085.       else
  1086.         {
  1087.           remove_breakpoints ();
  1088.           remove_step_breakpoint ();
  1089.           breakpoints_inserted = 0;
  1090.           another_trap = 1;
  1091.         }
  1092.       
  1093.       /* We come here if we hit a breakpoint but should not
  1094.          stop for it.  Possibly we also were stepping
  1095.          and should stop for that.  So fall through and
  1096.          test for stepping.  But, if not stepping,
  1097.          do not stop.  */
  1098.     }
  1099.       
  1100.       /* If this is the breakpoint at the end of a stack dummy,
  1101.      just stop silently.  */
  1102.       if (!random_signal 
  1103.      && PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address))
  1104.       {
  1105.         stop_print_frame = 0;
  1106.         stop_stack_dummy = 1;
  1107. #ifdef HP_OS_BUG
  1108.         trap_expected_after_continue = 1;
  1109. #endif
  1110.         break;
  1111.       }
  1112.       
  1113.       if (step_resume_break_address)
  1114.     /* Having a step-resume breakpoint overrides anything
  1115.        else having to do with stepping commands until
  1116.        that breakpoint is reached.  */
  1117.     ;
  1118.       /* If stepping through a line, keep going if still within it.  */
  1119.       else if (!random_signal
  1120.            && step_range_end
  1121.            && stop_pc >= step_range_start
  1122.            && stop_pc < step_range_end
  1123.            /* The step range might include the start of the
  1124.           function, so if we are at the start of the
  1125.           step range and either the stack or frame pointers
  1126.           just changed, we've stepped outside */
  1127.            && !(stop_pc == step_range_start
  1128.             && stop_frame_address
  1129.             && (stop_sp INNER_THAN prev_sp
  1130.             || stop_frame_address != step_frame_address)))
  1131.     {
  1132. #if 0
  1133.       /* When "next"ing through a function,
  1134.          This causes an extra stop at the end.
  1135.          Is there any reason for this?
  1136.          It's confusing to the user.  */
  1137.       /* Don't step through the return from a function
  1138.          unless that is the first instruction stepped through.  */
  1139.       if (ABOUT_TO_RETURN (stop_pc))
  1140.         {
  1141.           stop_step = 1;
  1142.           break;
  1143.         }
  1144. #endif
  1145.     }
  1146.       
  1147.       /* We stepped out of the stepping range.  See if that was due
  1148.      to a subroutine call that we should proceed to the end of.  */
  1149.       else if (!random_signal && step_range_end)
  1150.     {
  1151.       if (stop_func_start)
  1152.         {
  1153.           prologue_pc = stop_func_start;
  1154.           SKIP_PROLOGUE (prologue_pc);
  1155.         }
  1156.  
  1157.       /* Did we just take a signal?  */
  1158.       if (IN_SIGTRAMP (stop_pc, stop_func_name)
  1159.           && !IN_SIGTRAMP (prev_pc, prev_func_name))
  1160.         {
  1161.           /* This code is needed at least in the following case:
  1162.          The user types "next" and then a signal arrives (before
  1163.          the "next" is done).  */
  1164.           /* We've just taken a signal; go until we are back to
  1165.          the point where we took it and one more.  */
  1166.           step_resume_break_address = prev_pc;
  1167.           step_resume_break_duplicate =
  1168.         breakpoint_here_p (step_resume_break_address);
  1169.           if (breakpoints_inserted)
  1170.         insert_step_breakpoint ();
  1171.           /* Make sure that the stepping range gets us past
  1172.          that instruction.  */
  1173.           if (step_range_end == 1)
  1174.         step_range_end = (step_range_start = prev_pc) + 1;
  1175.           remove_breakpoints_on_following_step = 1;
  1176.         }
  1177.  
  1178.       /* ==> See comments at top of file on this algorithm.  <==*/
  1179.       
  1180.       else if (stop_pc == stop_func_start
  1181.           && (stop_func_start != prev_func_start
  1182.           || prologue_pc != stop_func_start
  1183.           || stop_sp != prev_sp))
  1184.         {
  1185.           /* It's a subroutine call */
  1186.           if (step_over_calls > 0 
  1187.           || (step_over_calls &&  find_pc_function (stop_pc) == 0))
  1188.         {
  1189.           /* A subroutine call has happened.  */
  1190.           /* Set a special breakpoint after the return */
  1191.           step_resume_break_address =
  1192.             ADDR_BITS_REMOVE
  1193.               (SAVED_PC_AFTER_CALL (get_current_frame ()));
  1194.           step_resume_break_duplicate
  1195.             = breakpoint_here_p (step_resume_break_address);
  1196.           if (breakpoints_inserted)
  1197.             insert_step_breakpoint ();
  1198.         }
  1199.           /* Subroutine call with source code we should not step over.
  1200.          Do step to the first line of code in it.  */
  1201.           else if (step_over_calls)
  1202.         {
  1203.           SKIP_PROLOGUE (stop_func_start);
  1204.           sal = find_pc_line (stop_func_start, 0);
  1205.           /* Use the step_resume_break to step until
  1206.              the end of the prologue, even if that involves jumps
  1207.              (as it seems to on the vax under 4.2).  */
  1208.           /* If the prologue ends in the middle of a source line,
  1209.              continue to the end of that source line.
  1210.              Otherwise, just go to end of prologue.  */
  1211. #ifdef PROLOGUE_FIRSTLINE_OVERLAP
  1212.           /* no, don't either.  It skips any code that's
  1213.              legitimately on the first line.  */
  1214. #else
  1215.           if (sal.end && sal.pc != stop_func_start)
  1216.             stop_func_start = sal.end;
  1217. #endif
  1218.           
  1219.           if (stop_func_start == stop_pc)
  1220.             {
  1221.               /* We are already there: stop now.  */
  1222.               stop_step = 1;
  1223.               break;
  1224.             }
  1225.           else
  1226.             /* Put the step-breakpoint there and go until there. */
  1227.             {
  1228.               step_resume_break_address = stop_func_start;
  1229.               
  1230.               step_resume_break_duplicate
  1231.             = breakpoint_here_p (step_resume_break_address);
  1232.               if (breakpoints_inserted)
  1233.             insert_step_breakpoint ();
  1234.               /* Do not specify what the fp should be when we stop
  1235.              since on some machines the prologue
  1236.              is where the new fp value is established.  */
  1237.               step_frame_address = 0;
  1238.               /* And make sure stepping stops right away then.  */
  1239.               step_range_end = step_range_start;
  1240.             }
  1241.         }
  1242.           else
  1243.         {
  1244.           /* We get here only if step_over_calls is 0 and we
  1245.              just stepped into a subroutine.  I presume
  1246.              that step_over_calls is only 0 when we're
  1247.              supposed to be stepping at the assembly
  1248.              language level.*/
  1249.           stop_step = 1;
  1250.           break;
  1251.         }
  1252.         }
  1253.       /* No subroutine call; stop now.  */
  1254.       else
  1255.         {
  1256.           /* We've wandered out of the step range (but we haven't done a
  1257.          subroutine call or return (that's handled elsewhere)).  We
  1258.          don't really want to stop until we encounter the start of a
  1259.          new statement.  If so, we stop.  Otherwise, we reset
  1260.          step_range_start and step_range_end, and just continue. */
  1261.           sal = find_pc_line(stop_pc, 0);
  1262.           
  1263.           if (current_line != sal.line
  1264.           && stop_pc == sal.pc) {
  1265.         stop_step = 1;
  1266.         break;
  1267.           } else {
  1268.         /* This is probably not necessary, but it probably makes
  1269.            stepping more efficient, as we avoid calling find_pc_line()
  1270.            for each instruction we step over. */
  1271.         step_range_start = sal.pc;
  1272.         step_range_end = sal.end;
  1273.           }
  1274.         }
  1275.     }
  1276.  
  1277.       else if (trap_expected
  1278.            && IN_SIGTRAMP (stop_pc, stop_func_name)
  1279.            && !IN_SIGTRAMP (prev_pc, prev_func_name))
  1280.     {
  1281.       /* What has happened here is that we have just stepped the inferior
  1282.          with a signal (because it is a signal which shouldn't make
  1283.          us stop), thus stepping into sigtramp.
  1284.  
  1285.          So we need to set a step_resume_break_address breakpoint
  1286.          and continue until we hit it, and then step.  */
  1287.       step_resume_break_address = prev_pc;
  1288.       /* Always 1, I think, but it's probably easier to have
  1289.          the step_resume_break as usual rather than trying to
  1290.          re-use the breakpoint which is already there.  */
  1291.       step_resume_break_duplicate =
  1292.         breakpoint_here_p (step_resume_break_address);
  1293.       if (breakpoints_inserted)
  1294.         insert_step_breakpoint ();
  1295.       remove_breakpoints_on_following_step = 1;
  1296.       another_trap = 1;
  1297.     }
  1298.  
  1299.       /* Save the pc before execution, to compare with pc after stop.  */
  1300.       prev_pc = read_pc ();    /* Might have been DECR_AFTER_BREAK */
  1301.       prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
  1302.                       BREAK is defined, the
  1303.                       original pc would not have
  1304.                       been at the start of a
  1305.                       function. */
  1306.       prev_func_name = stop_func_name;
  1307.       prev_sp = stop_sp;
  1308.  
  1309.       /* If we did not do break;, it means we should keep
  1310.      running the inferior and not return to debugger.  */
  1311.  
  1312.       if (trap_expected && stop_signal != SIGTRAP)
  1313.     {
  1314.       /* We took a signal (which we are supposed to pass through to
  1315.          the inferior, else we'd have done a break above) and we
  1316.          haven't yet gotten our trap.  Simply continue.  */
  1317.       resume ((step_range_end && !step_resume_break_address)
  1318.           || (trap_expected && !step_resume_break_address)
  1319.           || bpstat_should_step (),
  1320.           stop_signal);
  1321.     }
  1322.       else
  1323.     {
  1324.       /* Either the trap was not expected, but we are continuing
  1325.          anyway (the user asked that this signal be passed to the
  1326.          child)
  1327.            -- or --
  1328.          The signal was SIGTRAP, e.g. it was our signal, but we
  1329.          decided we should resume from it.
  1330.  
  1331.          We're going to run this baby now!
  1332.  
  1333.          Insert breakpoints now, unless we are trying
  1334.          to one-proceed past a breakpoint.  */
  1335.       /* If we've just finished a special step resume and we don't
  1336.          want to hit a breakpoint, pull em out.  */
  1337. #ifdef TDESC
  1338.           if (!tdesc_handle)
  1339.             {
  1340.           init_tdesc();
  1341.               safe_to_init_tdesc_context = 1;
  1342.             }
  1343. #endif
  1344.  
  1345.       if (!step_resume_break_address &&
  1346.           remove_breakpoints_on_following_step)
  1347.         {
  1348.           remove_breakpoints_on_following_step = 0;
  1349.           remove_breakpoints ();
  1350.           breakpoints_inserted = 0;
  1351.         }
  1352.       else if (!breakpoints_inserted &&
  1353.            (step_resume_break_address != NULL || !another_trap))
  1354.         {
  1355.           insert_step_breakpoint ();
  1356.           breakpoints_failed = insert_breakpoints ();
  1357.           if (breakpoints_failed)
  1358.         break;
  1359.           breakpoints_inserted = 1;
  1360.         }
  1361.  
  1362.       trap_expected = another_trap;
  1363.  
  1364.       if (stop_signal == SIGTRAP)
  1365.         stop_signal = 0;
  1366.  
  1367. #ifdef SHIFT_INST_REGS
  1368.       /* I'm not sure when this following segment applies.  I do know, now,
  1369.          that we shouldn't rewrite the regs when we were stopped by a
  1370.          random signal from the inferior process.  */
  1371.  
  1372.           if (!bpstat_explains_signal (stop_bpstat)
  1373.           && (stop_signal != SIGCLD) 
  1374.               && !stopped_by_random_signal)
  1375.             {
  1376.             CORE_ADDR pc_contents = read_register (PC_REGNUM);
  1377.             CORE_ADDR npc_contents = read_register (NPC_REGNUM);
  1378.             if (pc_contents != npc_contents)
  1379.               {
  1380.               write_register (NNPC_REGNUM, npc_contents);
  1381.               write_register (NPC_REGNUM, pc_contents);
  1382.           }
  1383.             }
  1384. #endif /* SHIFT_INST_REGS */
  1385.  
  1386.       resume ((step_range_end && !step_resume_break_address)
  1387.           || (trap_expected && !step_resume_break_address)
  1388.           || bpstat_should_step (),
  1389.           stop_signal);
  1390.     }
  1391.     }
  1392.   if (target_has_execution)
  1393.     {
  1394.       /* Assuming the inferior still exists, set these up for next
  1395.      time, just like we did above if we didn't break out of the
  1396.      loop.  */
  1397.       prev_pc = read_pc ();
  1398.       prev_func_start = stop_func_start;
  1399.       prev_func_name = stop_func_name;
  1400.       prev_sp = stop_sp;
  1401.     }
  1402. }
  1403.  
  1404. /* Here to return control to GDB when the inferior stops for real.
  1405.    Print appropriate messages, remove breakpoints, give terminal our modes.
  1406.  
  1407.    STOP_PRINT_FRAME nonzero means print the executing frame
  1408.    (pc, function, args, file, line number and line text).
  1409.    BREAKPOINTS_FAILED nonzero means stop was due to error
  1410.    attempting to insert breakpoints.  */
  1411.  
  1412. void
  1413. normal_stop ()
  1414. {
  1415.   /* Make sure that the current_frame's pc is correct.  This
  1416.      is a correction for setting up the frame info before doing
  1417.      DECR_PC_AFTER_BREAK */
  1418.   if (target_has_execution)
  1419.     (get_current_frame ())->pc = read_pc ();
  1420.   
  1421.   if (breakpoints_failed)
  1422.     {
  1423.       target_terminal_ours_for_output ();
  1424.       print_sys_errmsg ("ptrace", breakpoints_failed);
  1425.       printf ("Stopped; cannot insert breakpoints.\n\
  1426. The same program may be running in another process.\n");
  1427.     }
  1428.  
  1429.   if (target_has_execution)
  1430.     remove_step_breakpoint ();
  1431.  
  1432.   if (target_has_execution && breakpoints_inserted)
  1433.     if (remove_breakpoints ())
  1434.       {
  1435.     target_terminal_ours_for_output ();
  1436.     printf ("Cannot remove breakpoints because program is no longer writable.\n\
  1437. It might be running in another process.\n\
  1438. Further execution is probably impossible.\n");
  1439.       }
  1440.  
  1441.   breakpoints_inserted = 0;
  1442.  
  1443.   /* Delete the breakpoint we stopped at, if it wants to be deleted.
  1444.      Delete any breakpoint that is to be deleted at the next stop.  */
  1445.  
  1446.   breakpoint_auto_delete (stop_bpstat);
  1447.  
  1448.   /* If an auto-display called a function and that got a signal,
  1449.      delete that auto-display to avoid an infinite recursion.  */
  1450.  
  1451.   if (stopped_by_random_signal)
  1452.     disable_current_display ();
  1453.  
  1454.   if (step_multi && stop_step)
  1455.     return;
  1456.  
  1457.   target_terminal_ours ();
  1458.  
  1459.   if (!target_has_stack)
  1460.     return;
  1461.  
  1462.   /* Select innermost stack frame except on return from a stack dummy routine,
  1463.      or if the program has exited.  Print it without a level number if
  1464.      we have changed functions or hit a breakpoint.  Print source line
  1465.      if we have one.  */
  1466.   if (!stop_stack_dummy)
  1467.     {
  1468.       select_frame (get_current_frame (), 0);
  1469.  
  1470.       if (stop_print_frame)
  1471.     {
  1472.       int source_only;
  1473.  
  1474.       source_only = bpstat_print (stop_bpstat);
  1475.       source_only = source_only ||
  1476.             (   stop_step
  1477.          && step_frame_address == stop_frame_address
  1478.          && step_start_function == find_pc_function (stop_pc));
  1479.  
  1480.           print_stack_frame (selected_frame, -1, source_only? -1: 1);
  1481.  
  1482.       /* Display the auto-display expressions.  */
  1483.       do_displays ();
  1484.     }
  1485.     }
  1486.  
  1487.   /* Save the function value return registers, if we care.
  1488.      We might be about to restore their previous contents.  */
  1489.   if (proceed_to_finish)
  1490.     read_register_bytes (0, stop_registers, REGISTER_BYTES);
  1491.  
  1492.   if (stop_stack_dummy)
  1493.     {
  1494.       /* Pop the empty frame that contains the stack dummy.
  1495.          POP_FRAME ends with a setting of the current frame, so we
  1496.      can use that next. */
  1497.       POP_FRAME;
  1498.       select_frame (get_current_frame (), 0);
  1499.     }
  1500. }
  1501.  
  1502. static void
  1503. insert_step_breakpoint ()
  1504. {
  1505.   if (step_resume_break_address && !step_resume_break_duplicate)
  1506.     target_insert_breakpoint (step_resume_break_address,
  1507.                   step_resume_break_shadow);
  1508. }
  1509.  
  1510. static void
  1511. remove_step_breakpoint ()
  1512. {
  1513.   if (step_resume_break_address && !step_resume_break_duplicate)
  1514.     target_remove_breakpoint (step_resume_break_address,
  1515.                   step_resume_break_shadow);
  1516. }
  1517.  
  1518. static void
  1519. sig_print_header ()
  1520. {
  1521.   printf_filtered ("Signal\t\tStop\tPrint\tPass to program\tDescription\n");
  1522. }
  1523.  
  1524. static void
  1525. sig_print_info (number)
  1526.      int number;
  1527. {
  1528.   char *abbrev = sig_abbrev(number);
  1529.   if (abbrev == NULL)
  1530.     printf_filtered ("%d\t\t", number);
  1531.   else
  1532.     printf_filtered ("SIG%s (%d)\t", abbrev, number);
  1533.   printf_filtered ("%s\t", signal_stop[number] ? "Yes" : "No");
  1534.   printf_filtered ("%s\t", signal_print[number] ? "Yes" : "No");
  1535.   printf_filtered ("%s\t\t", signal_program[number] ? "Yes" : "No");
  1536.   printf_filtered ("%s\n", sys_siglist[number]);
  1537. }
  1538.  
  1539. /* Specify how various signals in the inferior should be handled.  */
  1540.  
  1541. static void
  1542. handle_command (args, from_tty)
  1543.      char *args;
  1544.      int from_tty;
  1545. {
  1546.   register char *p = args;
  1547.   int signum = 0;
  1548.   register int digits, wordlen;
  1549.   char *nextarg;
  1550.  
  1551.   if (!args)
  1552.     error_no_arg ("signal to handle");
  1553.  
  1554.   while (*p)
  1555.     {
  1556.       /* Find the end of the next word in the args.  */
  1557.       for (wordlen = 0;
  1558.        p[wordlen] && p[wordlen] != ' ' && p[wordlen] != '\t';
  1559.        wordlen++);
  1560.       /* Set nextarg to the start of the word after the one we just
  1561.      found, and null-terminate this one.  */
  1562.       if (p[wordlen] == '\0')
  1563.     nextarg = p + wordlen;
  1564.       else
  1565.     {
  1566.       p[wordlen] = '\0';
  1567.       nextarg = p + wordlen + 1;
  1568.     }
  1569.       
  1570.  
  1571.       for (digits = 0; p[digits] >= '0' && p[digits] <= '9'; digits++);
  1572.  
  1573.       if (signum == 0)
  1574.     {
  1575.       /* It is the first argument--must be the signal to operate on.  */
  1576.       if (digits == wordlen)
  1577.         {
  1578.           /* Numeric.  */
  1579.           signum = atoi (p);
  1580.           if (signum <= 0 || signum >= NSIG)
  1581.         {
  1582.           p[wordlen] = '\0';
  1583.           error ("Invalid signal %s given as argument to \"handle\".", p);
  1584.         }
  1585.         }
  1586.       else
  1587.         {
  1588.           /* Symbolic.  */
  1589.           signum = sig_number (p);
  1590.           if (signum == -1)
  1591.         error ("No such signal \"%s\"", p);
  1592.         }
  1593.  
  1594.       if (signum == SIGTRAP || signum == SIGINT)
  1595.         {
  1596.           if (!query ("SIG%s is used by the debugger.\nAre you sure you want to change it? ", sig_abbrev (signum)))
  1597.         error ("Not confirmed.");
  1598.         }
  1599.     }
  1600.       /* Else, if already got a signal number, look for flag words
  1601.      saying what to do for it.  */
  1602.       else if (!strncmp (p, "stop", wordlen))
  1603.     {
  1604.       signal_stop[signum] = 1;
  1605.       signal_print[signum] = 1;
  1606.     }
  1607.       else if (wordlen >= 2 && !strncmp (p, "print", wordlen))
  1608.     signal_print[signum] = 1;
  1609.       else if (wordlen >= 2 && !strncmp (p, "pass", wordlen))
  1610.     signal_program[signum] = 1;
  1611.       else if (!strncmp (p, "ignore", wordlen))
  1612.     signal_program[signum] = 0;
  1613.       else if (wordlen >= 3 && !strncmp (p, "nostop", wordlen))
  1614.     signal_stop[signum] = 0;
  1615.       else if (wordlen >= 4 && !strncmp (p, "noprint", wordlen))
  1616.     {
  1617.       signal_print[signum] = 0;
  1618.       signal_stop[signum] = 0;
  1619.     }
  1620.       else if (wordlen >= 4 && !strncmp (p, "nopass", wordlen))
  1621.     signal_program[signum] = 0;
  1622.       else if (wordlen >= 3 && !strncmp (p, "noignore", wordlen))
  1623.     signal_program[signum] = 1;
  1624.       /* Not a number and not a recognized flag word => complain.  */
  1625.       else
  1626.     {
  1627.       error ("Unrecognized flag word: \"%s\".", p);
  1628.     }
  1629.  
  1630.       /* Find start of next word.  */
  1631.       p = nextarg;
  1632.       while (*p == ' ' || *p == '\t') p++;
  1633.     }
  1634.  
  1635.   if (from_tty)
  1636.     {
  1637.       /* Show the results.  */
  1638.       sig_print_header ();
  1639.       sig_print_info (signum);
  1640.     }
  1641. }
  1642.  
  1643. /* Print current contents of the tables set by the handle command.  */
  1644.  
  1645. static void
  1646. signals_info (signum_exp)
  1647.      char *signum_exp;
  1648. {
  1649.   register int i;
  1650.   sig_print_header ();
  1651.  
  1652.   if (signum_exp)
  1653.     {
  1654.       /* First see if this is a symbol name.  */
  1655.       i = sig_number (signum_exp);
  1656.       if (i == -1)
  1657.     {
  1658.       /* Nope, maybe it's an address which evaluates to a signal
  1659.          number.  */
  1660.       i = parse_and_eval_address (signum_exp);
  1661.       if (i >= NSIG || i < 0)
  1662.         error ("Signal number out of bounds.");
  1663.     }
  1664.       sig_print_info (i);
  1665.       return;
  1666.     }
  1667.  
  1668.   printf_filtered ("\n");
  1669.   for (i = 0; i < NSIG; i++)
  1670.     {
  1671.       QUIT;
  1672.  
  1673.       sig_print_info (i);
  1674.     }
  1675.  
  1676.   printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
  1677. }
  1678.  
  1679. /* Save all of the information associated with the inferior<==>gdb
  1680.    connection.  INF_STATUS is a pointer to a "struct inferior_status"
  1681.    (defined in inferior.h).  */
  1682.  
  1683. void
  1684. save_inferior_status (inf_status, restore_stack_info)
  1685.      struct inferior_status *inf_status;
  1686.      int restore_stack_info;
  1687. {
  1688.   inf_status->pc_changed = pc_changed;
  1689.   inf_status->stop_signal = stop_signal;
  1690.   inf_status->stop_pc = stop_pc;
  1691.   inf_status->stop_frame_address = stop_frame_address;
  1692.   inf_status->stop_step = stop_step;
  1693.   inf_status->stop_stack_dummy = stop_stack_dummy;
  1694.   inf_status->stopped_by_random_signal = stopped_by_random_signal;
  1695.   inf_status->trap_expected = trap_expected;
  1696.   inf_status->step_range_start = step_range_start;
  1697.   inf_status->step_range_end = step_range_end;
  1698.   inf_status->step_frame_address = step_frame_address;
  1699.   inf_status->step_over_calls = step_over_calls;
  1700.   inf_status->step_resume_break_address = step_resume_break_address;
  1701.   inf_status->stop_after_trap = stop_after_trap;
  1702.   inf_status->stop_soon_quietly = stop_soon_quietly;
  1703.   /* Save original bpstat chain here; replace it with copy of chain. 
  1704.      If caller's caller is walking the chain, they'll be happier if we
  1705.      hand them back the original chain when restore_i_s is called.  */
  1706.   inf_status->stop_bpstat = stop_bpstat;
  1707.   stop_bpstat = bpstat_copy (stop_bpstat);
  1708.   inf_status->breakpoint_proceeded = breakpoint_proceeded;
  1709.   inf_status->restore_stack_info = restore_stack_info;
  1710.   inf_status->proceed_to_finish = proceed_to_finish;
  1711.   
  1712.   bcopy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
  1713.   
  1714.   record_selected_frame (&(inf_status->selected_frame_address),
  1715.              &(inf_status->selected_level));
  1716.   return;
  1717. }
  1718.  
  1719. void
  1720. restore_inferior_status (inf_status)
  1721.      struct inferior_status *inf_status;
  1722. {
  1723.   FRAME fid;
  1724.   int level = inf_status->selected_level;
  1725.  
  1726.   pc_changed = inf_status->pc_changed;
  1727.   stop_signal = inf_status->stop_signal;
  1728.   stop_pc = inf_status->stop_pc;
  1729.   stop_frame_address = inf_status->stop_frame_address;
  1730.   stop_step = inf_status->stop_step;
  1731.   stop_stack_dummy = inf_status->stop_stack_dummy;
  1732.   stopped_by_random_signal = inf_status->stopped_by_random_signal;
  1733.   trap_expected = inf_status->trap_expected;
  1734.   step_range_start = inf_status->step_range_start;
  1735.   step_range_end = inf_status->step_range_end;
  1736.   step_frame_address = inf_status->step_frame_address;
  1737.   step_over_calls = inf_status->step_over_calls;
  1738.   step_resume_break_address = inf_status->step_resume_break_address;
  1739.   stop_after_trap = inf_status->stop_after_trap;
  1740.   stop_soon_quietly = inf_status->stop_soon_quietly;
  1741.   bpstat_clear (&stop_bpstat);
  1742.   stop_bpstat = inf_status->stop_bpstat;
  1743.   breakpoint_proceeded = inf_status->breakpoint_proceeded;
  1744.   proceed_to_finish = inf_status->proceed_to_finish;
  1745.  
  1746.   bcopy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
  1747.  
  1748.   /* The inferior can be gone if the user types "print exit(0)"
  1749.      (and perhaps other times).  */
  1750.   if (target_has_stack && inf_status->restore_stack_info)
  1751.     {
  1752.       fid = find_relative_frame (get_current_frame (),
  1753.                  &level);
  1754.  
  1755.       /* If inf_status->selected_frame_address is NULL, there was no
  1756.      previously selected frame.  */
  1757.       if (fid == 0 ||
  1758.       FRAME_FP (fid) != inf_status->selected_frame_address ||
  1759.       level != 0)
  1760.     {
  1761. #if 0
  1762.       /* I'm not sure this error message is a good idea.  I have
  1763.          only seen it occur after "Can't continue previously
  1764.          requested operation" (we get called from do_cleanups), in
  1765.          which case it just adds insult to injury (one confusing
  1766.          error message after another.  Besides which, does the
  1767.          user really care if we can't restore the previously
  1768.          selected frame?  */
  1769.       fprintf (stderr, "Unable to restore previously selected frame.\n");
  1770. #endif
  1771.       select_frame (get_current_frame (), 0);
  1772.       return;
  1773.     }
  1774.       
  1775.       select_frame (fid, inf_status->selected_level);
  1776.     }
  1777. }
  1778.  
  1779.  
  1780. void
  1781. _initialize_infrun ()
  1782. {
  1783.   register int i;
  1784.  
  1785.   add_info ("signals", signals_info,
  1786.         "What debugger does when program gets various signals.\n\
  1787. Specify a signal number as argument to print info on that signal only.");
  1788.  
  1789.   add_com ("handle", class_run, handle_command,
  1790.        "Specify how to handle a signal.\n\
  1791. Args are signal number followed by flags.\n\
  1792. Flags allowed are \"stop\", \"print\", \"pass\",\n\
  1793.  \"nostop\", \"noprint\" or \"nopass\".\n\
  1794. Print means print a message if this signal happens.\n\
  1795. Stop means reenter debugger if this signal happens (implies print).\n\
  1796. Pass means let program see this signal; otherwise program doesn't know.\n\
  1797. Pass and Stop may be combined.");
  1798.  
  1799.   for (i = 0; i < NSIG; i++)
  1800.     {
  1801.       signal_stop[i] = 1;
  1802.       signal_print[i] = 1;
  1803.       signal_program[i] = 1;
  1804.     }
  1805.  
  1806.   /* Signals caused by debugger's own actions
  1807.      should not be given to the program afterwards.  */
  1808.   signal_program[SIGTRAP] = 0;
  1809.   signal_program[SIGINT] = 0;
  1810.  
  1811.   /* Signals that are not errors should not normally enter the debugger.  */
  1812. #ifdef SIGALRM
  1813.   signal_stop[SIGALRM] = 0;
  1814.   signal_print[SIGALRM] = 0;
  1815. #endif /* SIGALRM */
  1816. #ifdef SIGVTALRM
  1817.   signal_stop[SIGVTALRM] = 0;
  1818.   signal_print[SIGVTALRM] = 0;
  1819. #endif /* SIGVTALRM */
  1820. #ifdef SIGPROF
  1821.   signal_stop[SIGPROF] = 0;
  1822.   signal_print[SIGPROF] = 0;
  1823. #endif /* SIGPROF */
  1824. #ifdef SIGCHLD
  1825.   signal_stop[SIGCHLD] = 0;
  1826.   signal_print[SIGCHLD] = 0;
  1827. #endif /* SIGCHLD */
  1828. #ifdef SIGCLD
  1829.   signal_stop[SIGCLD] = 0;
  1830.   signal_print[SIGCLD] = 0;
  1831. #endif /* SIGCLD */
  1832. #ifdef SIGIO
  1833.   signal_stop[SIGIO] = 0;
  1834.   signal_print[SIGIO] = 0;
  1835. #endif /* SIGIO */
  1836. #ifdef SIGURG
  1837.   signal_stop[SIGURG] = 0;
  1838.   signal_print[SIGURG] = 0;
  1839. #endif /* SIGURG */
  1840. }
  1841.  
  1842.